package com.operator.Activitys.http.manager;

import android.app.Activity;
import android.util.Log;

import com.operator.Activitys.bean.HomeMenu;
import com.operator.Activitys.bean.Iccid;
import com.operator.Activitys.bean.ImageText;
import com.operator.Activitys.bean.InspectUpdateBean;
import com.operator.Activitys.bean.Install;
import com.operator.Activitys.bean.OrderState;
import com.operator.Activitys.bean.PrettySign;
import com.operator.Activitys.bean.Sale;
import com.operator.Activitys.bean.State;
import com.operator.Activitys.bean.Supply;
import com.operator.Activitys.bean.SupplyType;
import com.operator.Activitys.bean.User;
import com.operator.Activitys.http.ApiService;
import com.operator.Activitys.http.NetWorkMgr;
import com.operator.Activitys.http.api.HomeAPI;
import com.operator.Activitys.utils.Constant;

import org.simple.eventbus.EventBus;

import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 项目名称：
 * 类描述：所有 用户 接口管理 manager
 * 创建人：张飞祥
 * 创建时间：2018/5/9 0027 下午 1:33
 * 修改人：张飞祥
 * 修改时间：2018/5/9 0027 下午 1:33
 * 修改备注：
 */
public class HomeManager {

    private static HomeAPI mHomeAPI;
    private static ApiService mApiService;
    private static Activity mActivity;
    private static int mTaskId;

    //加载数据类型
    private int RefType = Constant.FIRST_LOAD;

    private static class HomeApiManagerHolder {
        private static final HomeManager INSTANCE = new HomeManager();
    }
    public static final HomeManager getHomeApiManager(Activity activity, int taskId) {
        mHomeAPI = NetWorkMgr.getInstance(activity)
                .getServiceAPI(HomeAPI.class);
        mApiService = ApiService.getApiService();
        mActivity = activity;
        mTaskId = taskId;
        return HomeApiManagerHolder.INSTANCE;
    }

    public void getType(int type){
        RefType=type;
    }

    /**
     * APP 检查更新接口
     */
    public void inspectUpdateApp(String appType) {
        Subscription subscription = mHomeAPI
                .inspectUpdateApp(appType).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<InspectUpdateBean>() {
                    @Override
                    public void onCompleted() {

                    }
                    @Override
                    public void onError(Throwable e) {
                        Log.e(mActivity.getClass().getSimpleName(),"structureList 错误" + e.toString());
                        //log 打印
                        EventBus.getDefault().post(e.toString(), Constant.BUS_APP_UPDATE_ERROR);
                    }
                    @Override
                    public void onNext(InspectUpdateBean bean) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(bean, Constant.BUS_APP_UPDATE);
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

    /**
     * 轮播图
     */
    public void homeBanner() {
        Subscription subscription = mHomeAPI
                .homeBanner().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<ImageText>>() {
                    @Override
                    public void onCompleted() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(e.toString(), Constant.BUS_HOME_BANNER_ERROR);
                        //log 打印
                        Log.e(mActivity.getClass().getSimpleName(),"structureList 错误" + e.toString());
                    }
                    @Override
                    public void onNext(List<ImageText> list) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(list, Constant.BUS_HOME_BANNER);
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

    /**
     * 广告
     */
    public void homeAdInfo() {
        Subscription subscription = mHomeAPI
                .homeAdInfo().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<ImageText>>() {
                    @Override
                    public void onCompleted() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(e.toString(), Constant.BUS_HOME_ADINFO_ERROR);
                        //log 打印
                        Log.e(mActivity.getClass().getSimpleName(),"structureList 错误" + e.toString());
                    }
                    @Override
                    public void onNext(List<ImageText> list) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(list.get(0), Constant.BUS_HOME_ADINFO);
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

    /**
     * 功能栏
     */
    public void homeMenu() {
        Subscription subscription = mHomeAPI
                .homeMenu().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<HomeMenu>>() {
                    @Override
                    public void onCompleted() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(e.toString(), Constant.BUS_HOME_MENU_ERROR);
                        //log 打印
                        Log.e(mActivity.getClass().getSimpleName(),"structureList 错误" + e.toString());
                    }
                    @Override
                    public void onNext(List<HomeMenu> list) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(list, Constant.BUS_HOME_MENU);
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

    /**
     * 获取 所有 iccid 列表
     */
    public void homeIccidList(String carrieroperator,int stroreId,String phoneNum) {
        Subscription subscription = mHomeAPI
                .homeIccidList(carrieroperator,stroreId,phoneNum).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<Iccid>>() {
                    @Override
                    public void onCompleted() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(e.toString(), Constant.BUS_HOME_ICCID_LIST_ERROR);
                        //log 打印
                        Log.e(mActivity.getClass().getSimpleName(),"structureList 错误" + e.toString());
                    }
                    @Override
                    public void onNext(List<Iccid> list) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(list, Constant.BUS_HOME_ICCID_LIST);
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

    /**
     * 宽带安装
     */
    public void homeInstall(String broadbandCategory) {
        Subscription subscription = mHomeAPI
                .homeInstall(broadbandCategory).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<Install>>() {
                    @Override
                    public void onCompleted() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(e.toString(), Constant.BUS_HOME_INSTALL_ERROR);
                        //log 打印
                        Log.e(mActivity.getClass().getSimpleName(),"structureList 错误" + e.toString());
                    }
                    @Override
                    public void onNext(List<Install> list) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(list, Constant.BUS_HOME_INSTALL);
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

    /**
     * 宽带安装 提交
     */
    public void homeInstallSubmit(int storeId,int broadBandInfoId,
                                  String broadbandInfoTitle,String broadbandCategory,
                                  String installAddress,String customerName,String linkTel,
                                  String IDCardNum,String account,String appointmentTime,
                                  String installType,double price,
                                  String IdCardBackImgUrl, String IdCardFrontImgUrl, String peoPleImgUrl,
                                  String remark, String addr, String birthday,String exp,
                                  String issuingUnit,String nation,String sex) {
        Subscription subscription = mHomeAPI
                .homeInstallSubmit( storeId, broadBandInfoId,
                 broadbandInfoTitle, broadbandCategory,
                 installAddress, customerName, linkTel,
                 IDCardNum, account, appointmentTime,
                 installType, price,
                 IdCardBackImgUrl,  IdCardFrontImgUrl,  peoPleImgUrl,
                 remark,  addr,  birthday, exp,
                 issuingUnit, nation, sex).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<OrderState>() {
                    @Override
                    public void onCompleted() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(e.toString(), Constant.BUS_HOME_INSTALL_SUBMIT_ERROR);
                        //log 打印
                        Log.e(mActivity.getClass().getSimpleName(),"structureList 错误" + e.toString());
                    }
                    @Override
                    public void onNext(OrderState bean) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(bean, Constant.BUS_HOME_INSTALL_SUBMIT);
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

    /**
     * 电话卡订单 信息提交 ------> 联通、170
     */
    public void homeUnciomSubmit(int storeId, String telephoneSetTitle,
                                 String customerName, double price, String remark,
                                 String carrieroperator,String telNumber,String IDCardNum,
                                 String iccid,String isPrettySign,String addr, String birthday, String exp,
                                 String issuingUnit,String nation,String sex,
                                 String IdCardBackImgUrl,  String IdCardFrontImgUrl,String peoPleImgUrl,String mailingAddress) {
        Subscription subscription = mHomeAPI
                .homeUnciomSubmit(storeId,telephoneSetTitle, customerName, price,remark,carrieroperator,
                        telNumber,IDCardNum,iccid,isPrettySign,addr,birthday,exp, issuingUnit,nation,sex,
                        IdCardBackImgUrl,IdCardFrontImgUrl, peoPleImgUrl,mailingAddress)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<OrderState>() {
                    @Override
                    public void onCompleted() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(e.toString(), Constant.BUS_HOME_UNICOM_SUBMIT_ERROR);
                        //log 打印
                        Log.e(mActivity.getClass().getSimpleName(),"structureList 错误" + e.toString());
                    }
                    @Override
                    public void onNext(OrderState bean) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(bean, Constant.BUS_HOME_UNICOM_SUBMIT);
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

    /**
     * 电话卡订单 信息提交 ------> 电信、移动
     */
    public void homeTelecomSubmit(int storeId, String telephoneSetTitle,String Carrieroperator,String telNumber,
                                 String iccid,String isPrettySign,String remark,double price) {
        Subscription subscription = mHomeAPI
                .homeTelecomSubmit(storeId, telephoneSetTitle, Carrieroperator, telNumber,
                        iccid, isPrettySign, remark,price)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<OrderState>() {
                    @Override
                    public void onCompleted() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(e.toString(), Constant.BUS_HOME_TELECOM_SUBMIT_ERROR);
                        //log 打印
                        Log.e(mActivity.getClass().getSimpleName(),"structureList 错误" + e.toString());
                    }
                    @Override
                    public void onNext(OrderState bean) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(bean, Constant.BUS_HOME_TELECOM_SUBMIT);
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }


    /**
     * 号卡销售 -------> 获取号卡列表
     */
    public void homeSaleList(String carrieroperator,int storeId,String numType,
                             int pageIndex,int pageSize) {
        Subscription subscription = mHomeAPI
                .homeSaleList(carrieroperator,storeId,numType,pageIndex,pageSize).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<Sale>>() {
                    @Override
                    public void onCompleted() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(e.toString(), Constant.BUS_HOME_SALE_LIST_ERROR);
                        //log 打印
                        Log.e(mActivity.getClass().getSimpleName(),"structureList 错误" + e.toString());
                    }
                    @Override
                    public void onNext(List<Sale> list) {

                        if(RefType== Constant.FIRST_LOAD){
                            //返回成功 首次加载数据处理
                            EventBus.getDefault().post(list, Constant.BUS_HOME_SALE_LIST);
                        }else{
                            //返回成功 上拉加载数据处理
                            EventBus.getDefault().post(list, Constant.BUS_HOME_SALE_LIST_FOOT);
                        }
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }


    /**
     * 号卡销售 -------> 获取号卡列表
     */
    public void homePrettyList(String carrieroperator,int storeId,int numType,
                             int pageIndex,int pageSize) {
        Subscription subscription = mHomeAPI
                .homePrettyList(carrieroperator,storeId,numType,pageIndex,pageSize).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<PrettySign>>() {
                    @Override
                    public void onCompleted() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(e.toString(), Constant.BUS_HOME_PRETTY_LIST_ERROR);
                        //log 打印
                        Log.e(mActivity.getClass().getSimpleName(),"structureList 错误" + e.toString());
                    }
                    @Override
                    public void onNext(List<PrettySign> list) {

                        if(RefType== Constant.FIRST_LOAD){
                            //返回成功 首次加载数据处理
                            EventBus.getDefault().post(list, Constant.BUS_HOME_PRETTY_LIST);
                        }else{
                            //返回成功 上拉加载数据处理
                            EventBus.getDefault().post(list, Constant.BUS_HOME_PRETTY_LIST_FOOT);
                        }
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

    /**
     * 添加通讯供求
     */
    public void homeAddSupply(String RFIsTypeId,String content,int storeId, String ImgSrc1, String ImgSrc2,
                               String ImgSrc3, String ImgSrc4, String ImgSrc5, String ImgSrc6) {
        Subscription subscription = mHomeAPI
                .homeAddSupply(RFIsTypeId,content,storeId,ImgSrc1,ImgSrc2, ImgSrc3,
                        ImgSrc4,ImgSrc5,ImgSrc6).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<State>() {
                    @Override
                    public void onCompleted() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(e.toString(), Constant.BUS_HOME_ADD_SUPPLY_ERROR);
                        //log 打印
                        Log.e(mActivity.getClass().getSimpleName(),"structureList 错误" + e.toString());
                    }
                    @Override
                    public void onNext(State bean) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(bean, Constant.BUS_HOME_ADD_SUPPLY);
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

    /**
     * 删除通讯供求
     */
    public void homeDelSupply(int RFIsId,int storeId) {
        Subscription subscription = mHomeAPI
                .homeDelSupply(RFIsId,storeId).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<State>() {
                    @Override
                    public void onCompleted() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(e.toString(), Constant.BUS_HOME_DEL_SUPPLY_ERROR);
                        //log 打印
                        Log.e(mActivity.getClass().getSimpleName(),"structureList 错误" + e.toString());
                    }
                    @Override
                    public void onNext(State bean) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(bean, Constant.BUS_HOME_DEL_SUPPLY);
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

    /**
     * 获取通讯供求分类
     */
    public void homeSupplyType() {
        Subscription subscription = mHomeAPI
                .homeSupplyType().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<SupplyType>>() {
                    @Override
                    public void onCompleted() {

                    }
                    @Override
                    public void onError(Throwable e) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(e.toString(), Constant.BUS_HOME_SUPPLY_TYPE_ERROR);
                        //log 打印
                        Log.e(mActivity.getClass().getSimpleName(),"structureList 错误" + e.toString());
                    }
                    @Override
                    public void onNext(List<SupplyType> list) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(list, Constant.BUS_HOME_SUPPLY_TYPE);
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

    /**
     * 获取通讯供求 信息
     */
    public void homeSupplyList(int storeId,String RFIsTypeId,String searchInput,int pageIndex, int pageSize) {
        Subscription subscription = mHomeAPI
                .homeSupplyList(storeId,RFIsTypeId,searchInput,pageIndex,pageSize).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<Supply>>() {
                    @Override
                    public void onCompleted() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(e.toString(), Constant.BUS_HOME_SUPPLY_LIST_ERROR);
                        //log 打印
                        Log.e(mActivity.getClass().getSimpleName(),"structureList 错误" + e.toString());
                    }
                    @Override
                    public void onNext(List<Supply> list) {

                        if(RefType== Constant.FIRST_LOAD){
                            //返回成功 首次加载数据处理
                            EventBus.getDefault().post(list, Constant.BUS_HOME_SUPPLY_LIST);
                        }else{
                            //返回成功 上拉加载数据处理
                            EventBus.getDefault().post(list, Constant.BUS_HOME_SUPPLY_LIST_FOOT);
                        }
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

    /**
     * 获取 我的通讯供求 信息
     */
    public void homeMySupplyList(int storeId,String RFIsTypeId,String searchInput,int pageIndex, int pageSize) {
        Subscription subscription = mHomeAPI
                .homeSupplyList(storeId,RFIsTypeId,searchInput,pageIndex,pageSize).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<Supply>>() {
                    @Override
                    public void onCompleted() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(e.toString(), Constant.BUS_HOME_MY_SUPPLY_LIST_ERROR);
                        //log 打印
                        Log.e(mActivity.getClass().getSimpleName(),"structureList 错误" + e.toString());
                    }
                    @Override
                    public void onNext(List<Supply> list) {

                        if(RefType== Constant.FIRST_LOAD){
                            //返回成功 首次加载数据处理
                            EventBus.getDefault().post(list, Constant.BUS_HOME_MY_SUPPLY_LIST);
                        }else{
                            //返回成功 上拉加载数据处理
                            EventBus.getDefault().post(list, Constant.BUS_HOME_MY_SUPPLY_LIST_FOOT);
                        }
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

    /**
     * 获取 搜索 我的通讯供求 信息
     */
    public void homeSearchSupplyList(int storeId,String RFIsTypeId,String searchInput,int pageIndex, int pageSize) {
        Subscription subscription = mHomeAPI
                .homeSupplyList(storeId,RFIsTypeId,searchInput,pageIndex,pageSize).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<Supply>>() {
                    @Override
                    public void onCompleted() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(e.toString(), Constant.BUS_HOME_SEARCH_SUPPLY_LIST_ERROR);
                        //log 打印
                        Log.e(mActivity.getClass().getSimpleName(),"structureList 错误" + e.toString());
                    }
                    @Override
                    public void onNext(List<Supply> list) {

                        if(RefType== Constant.FIRST_LOAD){
                            //返回成功 首次加载数据处理
                            EventBus.getDefault().post(list, Constant.BUS_HOME_SEARCH_SUPPLY_LIST);
                        }else{
                            //返回成功 上拉加载数据处理
                            EventBus.getDefault().post(list, Constant.BUS_HOME_SEARCH_SUPPLY_LIST_FOOT);
                        }
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

    /**
     * 根据 店铺 名称 获取 店铺信息
     */
    public void homeSupplyDetails(String storeName) {
        Subscription subscription = mHomeAPI
                .homeSupplyDetails(storeName).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<User>() {
                    @Override
                    public void onCompleted() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(e.toString(), Constant.BUS_HOME_SUPPLY_DETAILS_ERROR);
                        //log 打印
                        Log.e(mActivity.getClass().getSimpleName(),"structureList 错误" + e.toString());
                    }
                    @Override
                    public void onNext(User bean) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(bean, Constant.BUS_HOME_SUPPLY_DETAILS);
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

}
